package form.AccountForm;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JTabbedPane;

import customGUI.selectors.AccountSelector;

import other.CodeDescriptionPair;
import other.ObservableJTabbedPane;


import db.Account;
import form.Form;
import form.FormInputComponent;

public class AccountForm extends Form implements Observer{
	
	private AccountSelector selector;
	FormInputComponent userName;
	FormInputComponent password;
	FormInputComponent acctType;

	AccountInfoForm accountInfoForm;

	public FormInputComponent getAcctType() {
		return acctType;
	}

	public void setAcctType(FormInputComponent acctType) {
		this.acctType = acctType;
	}
	
	public AccountForm(){
		accountInfoForm = new AccountInfoForm();
	}
	
	public AccountInfoForm getAccountInfoForm() {
		return accountInfoForm;
	}

	public void setAccountInfoForm(AccountInfoForm accountInfoForm) {
		this.accountInfoForm = accountInfoForm;
	}

	public FormInputComponent getUserName() {
		return userName;
	}

	public void setUserName(FormInputComponent userName) {
		int prevIndex = -1;
		if(this.userName != null)
		{
			prevIndex = getComponentIndex(this.userName);
			this.remove(this.userName);
		}
		this.userName = userName;
		if(prevIndex >= 0)
			this.add(userName, prevIndex);
		else
			this.add(userName);
	}
	
	private int getComponentIndex(FormInputComponent component){
		Component[] components = this.getComponents();
		for(int i = 0; i<components.length;i++)
			if(components[i].equals(component))
				return i;
		return -1;
	}

	public FormInputComponent getPassword() {
		return password;
	}
	public AccountSelector getSelector() {
		return selector;
	}
	public void setSelector(AccountSelector selector) {
		this.selector = selector;
	}

	public void setPassword(FormInputComponent password) {
		this.password = password;
	}
	public void populate(String username){
		Account account = Account.createAccount(username);
		populate(account);
	}
	
	public void populate(Account account){
		if(account != null){
			userName.setInput(account.getUsername());
			accountInfoForm.getFName().setInput(account.getFirstName());
			accountInfoForm.getLName().setInput(account.getLastName());
			acctType.setInput(account.getAcctTypeDesc());
		}
	}
	
	public ArrayList<FormInputComponent> getInputComponents() {
		return inputComponents;
	}
	
	public void addComponents(){
		inputComponents.add(userName);
		inputComponents.add(password);
		inputComponents.add(accountInfoForm.getFName());
		inputComponents.add(accountInfoForm.getLName());
	}

	
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
			JTabbedPane tabbedPane = ((ObservableJTabbedPane) arg0).getJTabbedPane();
			
			if(tabbedPane.getSelectedComponent() == this && selector != null)
			{
				selector.refreshAssetChoices();
			}
	}
	
	public void updateAccountInfo(Account account){
		account.setFirstName(accountInfoForm.getFName().getInput());
		account.setLastName(accountInfoForm.getLName().getInput());
		account.setPassword(password.getInput());
		account.setAcctTypeCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_USER_TYPES, acctType.getInput()));
		
	}

}

